package cn.wolfcode.wolf2w.service.impl;


import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.query.RegionQuery;
import cn.wolfcode.wolf2w.repository.RegionRepository;
import cn.wolfcode.wolf2w.service.IRegionService;
import cn.wolfcode.wolf2w.util.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class RegionServiceImpl implements IRegionService {


    //在这里引用后期后台项目引用会导致报错
   /* @Value("${sms.url}")
    private String smsUrl;
    @Value("${sms.Key")
    private String appKey;*/


    @Autowired
    private RegionRepository repository;
    @Autowired
    private MongoTemplate template;

    @Override
    public void save(Region region) {
        repository.save(region);
    }

    @Override
    public void update(Region region) {
        Region region1 = repository.findById(region.getId()).get();
        region1.setInfo(region.getInfo());
        region1.setIshot(region.getIshot());
        region1.setName(region.getName());
        region1.setRefIds(region.getRefIds());
        region1.setSequence(region.getSequence());
        region1.setSn(region.getSn());
        repository.save(region1);
    }


    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public Region get(String id) {
        Region region = repository.findById(id).get();
        return region;
    }

    @Override
    public List<Region> list() {
        List<Region> regions = repository.findAll();
        return regions;
    }

    //分页查询
    @Override
    public Page<Region> query(RegionQuery qo) {
        Query query=new Query();
        //repository只能针对所有数据进行分页
        //template不能直接对数据进行分页
        //使用template间接分页
        //pageResult 核心字段
        //totalCount 总记录数
        long totalcount = template.count(query, Region.class);
        if (totalcount==0){
            //空的分页对象
            return Page.empty();
        }
        //list 当前页显示数据集合
        Pageable pageable= PageRequest.of(qo.getCurrentPage()-1,qo.getPageSize(), Sort.Direction.ASC,"_id");
        query.with(pageable);
        List<Region> list = template.find(query, Region.class);
        return new PageImpl<>(list,pageable,totalcount);
    }

    @Override
    public void saveOrUpdate(Region region) {
        if(StringUtils.hasLength(region.getId())){
            this.update(region);
        }else {
            this.save(region);
        }
    }

    @Override
    public void changeHotValue(String id, int hot) {
        Region region = this.get(id);
        region.setIshot(hot);
        this.update(region);
    }

    @Override
    public List<Region> queryHotRegion() {
        //状态热门的
        List<Region> list=repository.findByIshotOrderBySequence(Region.STATE_HOT);
        //通过序号排序
        return list;
    }
}
